package parspack;

import java.util.ArrayList;
import java.util.List;
import org.eclipse.jdt.core.JavaModelException;

public class TypesCreator {
	private List<TypeElement> listTypeElements = new ArrayList<TypeElement>();
	public List<Posting> listPostings = new ArrayList<Posting>();

	public TypesCreator(List<MyCompilationUnit> compUnits)
			throws JavaModelException {
		for (MyCompilationUnit icu : compUnits) {
			List<String> list = icu.getTypesName();
			for (String name : list) {
				listTypeElements
						.add(new TypeElement(name, icu.getElementName()));
			}
		}
		for (TypeElement tp : listTypeElements) {
			Posting post = new Posting(tp.name, tp.path);
			int id = -1;
			for (int i1 = 0; i1 < listPostings.size(); i1++)
				if (listPostings.get(i1).equals(post)) {
					id = i1;
					break;
				}
			if (id != -1) {
				Posting match = listPostings.get(id);
				if (!match.path.contains(tp.path)) {
					match.addPath(tp.path);
				}
				listPostings.remove(id);
				listPostings.add(match);
			} else {
				listPostings.add(post);
			}
			
		}
	}

}

class TypeElement {
	// class for Type:Path
	String name;
	String path;

	TypeElement(String arg, String src) {
		name = arg;
		path = src;
	}
}